
//
//  YJDAuthenticateViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMRZViewController.h"
#import "XOWMRZView.h"
#import "XOWMIDCartCell.h"
#import "XOWMRequest.h"
@interface XOWMRZViewController ()<YJDRZViewDelegate,YJDRequestDelegate>
@property (nonatomic, strong) XOWMRZView *RZView;
@property (nonatomic, strong) NSArray *list;
@property (nonatomic, assign) int selectRow;
@property (nonatomic, strong) XOWMRequest *request;
@end

@implementation XOWMRZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = @[@"身份证",@"港澳台通行证",@"港澳台身份证",@"护照",@"军人/警察身份证"];
    self.selectRow = (int)self.list.count + 1;
    // Do any additional setup after loading the view.
}

- (XOWMRZView *)RZView
{
    if (!_RZView)
    {
        _RZView = [[XOWMRZView alloc] init];
        _RZView.delegate = self;
        [self.view addSubview:_RZView];
        _RZView.c_center.equal(self.view.c_right);
        _RZView.c_size.equal(self.RZView.bounds.size);
    }
    return _RZView;
}

- (XOWMRequest *)request
{
    if (!_request)
    {
        _request = [[XOWMRequest alloc]init];
        _request.delegate = self;
    }
    return _request;
}

#pragma mark - YJDRZViewDelegate
- (void)yjd_actionForYJDRZViewWithEvent:(YJDRZViewEvent)event dict:(NSDictionary *)dict
{
    if (event == YJDRZViewEventBack)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (event == YJDRZViewEventAuthenticate)
    {
        if ([self.RZView.nameTF.text length] < 1) return [CCAlert showMessage:@"姓名不能为空" hidden:nil];
        else if ([self.RZView.IDTF.text length] < 1) return [CCAlert showMessage:@"证件类型不能为空" hidden:nil];
        else if ([self.RZView.numTF.text length] < 1) return [CCAlert showMessage:@"证件号码不能为空" hidden:nil];
        else if ([self.RZView.IDTF.text isEqualToString:@"身份证"] && ![self.RZView.numTF.text yjd_isIDCart])
            return [CCAlert showMessage:@"请输入正确的证件号码" hidden:nil];
        else if (![self.RZView.nameTF.text yjd_isChinese] && ![self.RZView.nameTF.text yjd_isNumberOralphabet])
            return [CCAlert showMessage:@"姓名格式不正确" hidden:nil];
        [CCHud startHud];
        [self.request yjd_authenticateRequest:dict];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    tableView.c_height.equal(30 * 3);
    return [self.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identenfier = [NSString stringWithFormat:@"XOWMIDCartCell%ld",(long)indexPath.row];
    XOWMIDCartCell *cell = [tableView dequeueReusableCellWithIdentifier:identenfier];
    if (!cell)
    {
        cell = [[XOWMIDCartCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identenfier];
    }
    cell.title = self.list[indexPath.row];
    cell.select = (self.selectRow == indexPath.row)?YES:NO;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XOWMIDCartCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    self.RZView.IDTF.text = cell.title;
    self.RZView.IDTF.rightBtn.selected = !self.RZView.IDTF.rightBtn.selected;
    self.RZView.IDTF.rightBtn.customIMG.imageName = (self.RZView.IDTF.rightBtn.selected)?@"up":@"down";
    self.selectRow = (int)indexPath.row;
    tableView.hidden = YES;
}

#pragma mark - YJDRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict
{
    //关闭菊花
    [CCHud stopHud];
    
    if (!dict)
        return;
    
    int code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [dict valueForKey:@"info"];
    //成功
    if (code == 100)
    {
        [XOWMHelper yjd_setupAuthenticate:@"1"];
        [CCAlert showMessage:info hidden:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    //失败
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.RZView.titleIMG.content = ([XOWMHelper shareHelp].style == XOWMPlatformStyleImage)?@"yjd_认证_title":@"实名认证";
    self.RZView.nameTF.placeholder = @"请输入真实姓名";
    self.RZView.IDTF.placeholder = @"请选择证件类型";
    self.RZView.numTF.placeholder = @"请输入证件号码";
    self.RZView.authenticateBtn.content = ([XOWMHelper shareHelp].style == XOWMPlatformStyleImage)?@"yjd_认证_Btn":@"认证";
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_getMediaFailed {
    NSLog(@"Get User Succrss");
}
@end
